/**
 * Date: 2004-4-10
 * Time: 19:47:10
 */

package org.cnmarc4j.cnmarc;

/**
 * <p><code>Verifier</code> checks tags and data elements.</p>
 *
 * <p><b>Note:</b> Currently a tag is only checked for it's length
 * and data elements are checked for MARC control characters.</p>
 */
public class Verifier {

    private static final char US = MarcConstants.US;

    private static final char FT = MarcConstants.FT;

    private static final char RT = MarcConstants.RT;

    private Verifier() {}

    /**
     * <p>Checks if the tag is a valid tag name.</p>
     *
     * @param tag the tag name to check
     * @throws IllegalTagException if the tag is not valid
     */
    public static void checkTag(String tag) {
        if (tag.length() != 3)
            throw new IllegalTagException
                    (tag, "not a variable field identifier");
        return;
    }

    /**
     * <p>Checks if the data element does not contain control charecters.</p>
     *
     * @param data the characters to check
     * @throws IllegalDataElementException if the data element
     *                                     contains control characters
     */
    public static void checkDataElement(char[] data) {
        int len = data.length;
        if (len == 0)
            return;
        int i = 0;
        do {
            checkDataElement(data[i]);
        } while (++i < len);
    }

    /**
     * <p>Checks if the data element does not contain control charecters.</p>
     *
     * @param ch - the character to check
     * @throws IllegalDataElementException if the data element
     *                                     contains control characters
     */
    public static void checkDataElement(char ch) {
        switch (ch) {
            case RT :
                throw new IllegalDataElementException
                        ("Invalid character: record terminator");
            case FT :
                throw new IllegalDataElementException
                        ("Invalid character: field Terminator");
            case US :
                throw new IllegalDataElementException
                        ("Invalid character: subfield code identifier");
            default :
                return;
        }
    }
}
